package com.itheima.test08;

import java.io.*;
import java.net.Socket;

/*
发送端
读取本地文件

将本地文件上传至 服务器

从服务器获取结果
打印结果

释放资源


 */
public class ClientDemo {


    public static void main(String[] args) throws IOException {
        Runnable runnable = new Runnable() {
            public void run() {
                BufferedInputStream bis = null;
                Socket socket = null;
                try {
                    //读取本地文件
                    bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Administrator\\Desktop\\美女2.png"));
                    //将本地文件上传至 服务器
                    //获取输出流
                    socket = new Socket("127.0.0.1", 8888);
                    OutputStream os = socket.getOutputStream();
                    byte[] bytes= new byte[1024];
                    int len;
                    while ((len=bis.read(bytes))!=-1){
                        os.write(bytes,0,len);
                    }
                    //来个结束标记
                    socket.shutdownOutput();
                    //从服务器获取结果
                    InputStream is = socket.getInputStream();
                    //字节 -> 字符
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    String s;
                    while ((s=reader.readLine())!=null){
                        //打印结果
                        System.out.println(s);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    //释放资源
                    if (bis!=null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (socket!=null) {
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        };
        for (int i = 0; i < 100; i++) {
            new Thread(runnable).start();
        }



    }
}
